package view;

import controller.GroupChatController;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class CreateGroupView {
    private final GroupChatController controller = new GroupChatController();

    public void showUi() {
        JFrame jf = new JFrame("创建群聊");
        jf.setIconImage(Toolkit.getDefaultToolkit().createImage(MainView.class.getResource("qq.jpg")));
        JPanel jp = new JPanel();
        JTextField jtf = new JTextField();
        JLabel jLabel = new JLabel("请输入所创群聊的名称");
        jLabel.setFont(new Font("黑体", Font.BOLD, 25));
        JButton bu1 = new JButton("确      认");
        jf.getRootPane().setDefaultButton(bu1);
        JButton bu4 = new JButton("退      出");
        jp.setLayout(null);
        jtf.setBounds(125, 100, 250, 50);
        jtf.setFont(new Font("黑体", Font.BOLD, 25));
        jLabel.setBounds(125, 25, 300, 50);
        bu1.setBounds(150, 200, 150, 50);
        bu4.setBounds(150, 300, 150, 50);
        jp.add(jLabel);
        jf.add(bu1);
        jf.add(bu4);
        jp.add(jtf);
        jf.add(jp);
        jf.setVisible(true);
        jf.setBounds(600, 200, 500, 500);
        bu1.addActionListener(e -> {
            String text = jtf.getText();
            boolean b = controller.addGroup(text);
            if (b) {
                jtf.setText("");
                JOptionPane.showMessageDialog(null, "创建成功");
            } else {
                JOptionPane.showMessageDialog(null, "该群名已被使用");
            }
        });
        bu4.addActionListener(e -> jf.dispose());
        jf.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                jf.dispose();
            }
        });
    }
}